package net.asciiworks.filesyncr.controller {
    import flash.filesystem.File;
    
    import mx.collections.ArrayCollection;
    
    import net.asciiworks.filesyncr.ApplicationFacade;
    import net.asciiworks.filesyncr.model.DataProxy;
    import net.asciiworks.filesyncr.view.ApplicationMediator;
    import net.asciiworks.filesyncr.view.contentPane.StatusMediator;
    
    import org.puremvc.as3.interfaces.INotification;
    import org.puremvc.as3.patterns.command.SimpleCommand;

    public class CompareDirectoriesCommand extends SimpleCommand {
		
		private var data:DataProxy;
		private var status:StatusMediator;
		
        public function CompareDirectoriesCommand() {
            super();
			data = facade.retrieveProxy(DataProxy.NAME) as DataProxy;
			status = facade.retrieveMediator(StatusMediator.NAME) as StatusMediator;
        }

        override public function execute(note:INotification):void {
			if (data.source != "*" && data.destination != "*") {
				var source:ArrayCollection = getAllPaths(new File(data.source));
				status.listFiles(source,null);
			}
        }
		
		private function getAllPaths(directory:File, relativeDirectory:File=null):ArrayCollection {
			var paths:ArrayCollection = new ArrayCollection();
			
			for each (var file:File in directory.getDirectoryListing()) {
				if (file.isDirectory) {
					paths.addAll(getAllPaths(file));
				}
				else {
					paths.addItem(file);
				}
			}
			
			return paths;
		}
    }
}